package cn.com.max.springboot1.test.controller;

import cn.com.max.springboot1.test.beans.User;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-9-1.
 */
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index(){
        return "Hello Spring Boot";
    }

    /*
    RESTful 风格示例 USER
    要获取 request 对象的时候使用的是 HttpServletRequest 下面的对象
    * */

    @ResponseBody
    @GetMapping("/user")
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setUserName("张三");
        user.setUserPass("74B3VC412T2");
        user.setPhone("17361600710");

        return user;
    }

    @ResponseBody
    @GetMapping()
    public List<User> getUsers(){
        List<User> list = new ArrayList<User>();

        User user1 = new User();
        User user2 = new User();

        user1.setId(1);
        user1.setUserName("张三");
        user1.setUserPass("74B3VC412T2");
        user1.setPhone("17361600710");
        user2.setId(1);
        user2.setUserName("张三");
        user2.setUserPass("74B3VC412T2");
        user2.setPhone("17361600710");

        list.add(user1);
        list.add(user2);

        return list;
    }

    @ResponseBody
    @PostMapping("/user")
    public String addUser(HttpServletRequest request){
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String userpass = request.getParameter("userpass");
        String phone = request.getParameter("phone");

        return "info:"+id+"---"+username+"---"+userpass+"---"+phone;
    }

    @ResponseBody
    @DeleteMapping("/user")
    public String deleteUser(HttpServletRequest req){
        String id = req.getParameter("id");
        return "the user we deleted id is "+ id;
    }

    @ResponseBody
    @PutMapping("/user")
    public String modifyUser(HttpServletRequest req){
        String id = req.getParameter("id");
        return "modify id is "+id;
    }

    // 还有一种情况：就是通过 / 的方式跟着一个参数
    @GetMapping("/user/{id}")
    public String getParams(@PathVariable String id){
        return "获取到的参数是"+id;

    }


}
